package com.pjb.springbootjjwt.store.utils;

import com.pjb.springbootjjwt.store.entity.Compare;
import com.pjb.springbootjjwt.store.entity.Line;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListUtil {

    public static boolean compare(List<String> list, String c) {
        return list.contains(c);
    }

    public static List<String> eq(List<String> list, String c) throws Exception {
        List<String> r = new ArrayList<>();
        if (!list.isEmpty())
            for (String v : list) {
                if (Compare.eq(v, c)) {
                    r.add(v);
                }
            }
        return r;
    }

    public static List<String> eq(List<String> list, List<String> c) throws Exception {
        List<String> r = new ArrayList<>();
        if (!list.isEmpty())
            for (String v : list) {
                if (v != null) {
                    r.addAll(eq(c, v));
                }
            }
        return r;
    }

    public static List<String> noteq(List<String> list, String c) throws Exception {
        List<String> r = new ArrayList<>();
        if (!list.isEmpty())
            for (String v : list) {
                if (Compare.eq(v, c)) {
                    return r;
                }
            }
        r.add(c);
        return r;
    }

    public static List<String> noteq(List<String> list, List<String> c) throws Exception {
        List<String> r = new ArrayList<>();
        if (!list.isEmpty())
            for (String v : list) {
                if (v != null) {
                    r.addAll(noteq(c, v));
                }
            }
        return r;
    }

    public static List<String> greater(List<String> list, String c) throws Exception {
        List<String> r = new ArrayList<>();
        if (!list.isEmpty())
            for (String v : list) {
                if (Compare.gt(v, c)) {
                    r.add(v);
                }
            }
        return r;
    }

    public static List<String> greatEq(List<String> list, String c) throws Exception {
        List<String> r = new ArrayList<>();
        if (!list.isEmpty())
            for (String v : list) {
                if (Compare.gte(v, c)) {
                    r.add(v);
                }
            }
        return r;
    }

    public static List<String> litter(List<String> list, String c) throws Exception {
        List<String> r = new ArrayList<>();
        if (!list.isEmpty())
            for (String v : list) {
                if (Compare.lt(v, c)) {
                    r.add(v);
                }
            }
        return r;
    }

    public static List<String> litteEq(List<String> list, String c) throws Exception {
        List<String> r = new ArrayList<>();
        if (!list.isEmpty())
            for (String v : list) {
                if (Compare.lte(v, c)) {
                    r.add(v);
                }
            }
        return r;
    }

    public static List<String> eq2(List<String> list, List<String> c) throws Exception {
        List<String> r = new ArrayList<>();
        if (!list.isEmpty())
            for (String v : list) {
                if (v != null) {
                    r.addAll(ListUtil.eq(c, v));
                }
            }
        return r;
    }

    public static List<String> eq2(List<String> list, String c) throws Exception {
        List<String> r = new ArrayList<>();
        if (!list.isEmpty())
            for (String v : list) {
                if (Compare.eq(v, c)) {
                    r.add(v);
                }
            }
        return r;
    }

    /**
     * 多条记录和一条记录进行eq匹配
     *
     * @param list
     * @param i
     * @param to
     * @param j
     * @return
     * @throws Exception
     */
    public static List<Line> eqLine(List<Line> list, int i, Line to, int j) throws Exception {
        List<Line> r = new ArrayList<>();
        if (!list.isEmpty())
            for (Line line : list) {
                String v = line.getData().get(i);
                String c = to.getData().get(j);
                if (Compare.eq(v, c)) {
                    Line l = new Line();
                    List<String> data = line.getData();
                    data.addAll(to.getData());
                    String id = StringUtil.md5(FileUtil.jsonStr(data));
                    l.setKey(id);
                    l.setData(data);
                    r.add(l);
                }
            }
        return r;
    }

    public static List<Line> eqLine(List<Line> from, int i, List<Line> to, int j) throws Exception {
        List<Line> r = new ArrayList<>();
        if (!from.isEmpty() && !to.isEmpty()) {
            for (Line v : from) {
                if (v != null) {
                    r.addAll(eqLine(to, j, v, i));
                }
            }
        }
        return r;
    }

    public static void main(String[] args) throws Exception {
        List list = Arrays.asList("a1", "ab", "z");
        boolean b = compare(list, "z");
        System.out.println("b:" + b);

        List list2 = Arrays.asList("a1", "ab", "zz");
        System.out.println("eq:" + eq(list2, "ab"));
        System.out.println("eq:" + eq(list2, list));
        System.out.println("noteq:" + noteq(list2, list));
        System.out.println("noteq:" + noteq(list, list2));
    }
}
